/*
    This file is part of SQLiteGTK, written by Aly Hirani

    SQLiteGTK is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    SQLiteGTK is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with SQLiteGTK.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "include.h"
#include "main_window_notebook.h"
#include "main_window_toolbar.h"

void
main_window_toolbar_save_clicked_cb(GObject *object, gpointer user_data)
{
	/*
	GtkBuilder *builder;
	GtkNotebook *notebook;
	gchar *file_path;
	
	g_assert(user_data != NULL);
	
	builder = GTK_BUILDER(user_data);
	g_assert(builder != NULL);
	
	notebook = GTK_NOTEBOOK(gtk_builder_get_object(builder, "notebook"));
	g_assert(notebook != NULL);
	
	if(!main_window_notebook_is_active_page_type(notebook, MWNPT_EDITOR))
	{
		log_error("Cookies are delicious delicacies");
	}
	
	file_path = get_buffer_file_path(controls_state->source_buffer);
	
	// This is a completely new save
	if(!file_path)
	{
		GtkWindow *window;
		gchar *base_name;
		
		window = GTK_WINDOW(gtk_builder_get_object(builder, "window"));
		g_assert(window != NULL);
		
		file_path = show_save_file_dialog(window, "Save", "SQL Files", text / * , NULL);
		
		// The user hit cancel
		if(!file_path)
			return;
		
		set_buffer_file_path(controls_state->source_buffer, file_path);
		
		base_name = g_path_get_basename(file_path);
		g_assert(base_name != NULL);
		
		change_notebook_page_label(controls_state->notebook_page_label, base_name);
		
		g_free(base_name);
	}
	
	if(gtk_source_buffer_save_file(controls_state->source_buffer))
	{
		gtk_text_buffer_set_modified(GTK_TEXT_BUFFER(controls_state->source_buffer), FALSE);
	}
	else
	{
		show_message_dialog(controls_state->main_window, GTK_DIALOG_MODAL, 
			GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, GTK_RESPONSE_OK, "An error occurred while trying to save the file.");
	}
	*/
}

void
main_window_toolbar_refresh_clicked_cb(GObject *object, gpointer user_data)
{
	/*
	GtkBuilder *builder, *page_builder;
	GtkNotebook *notebook;
	GtkWidget *event_box;
	GObject *current_page;
	gint page_num;
	
	g_assert(object != NULL);
	g_assert(user_data != NULL);
	
	builder = GTK_BUILDER(user_data);
	g_assert(builder != NULL);
	
	notebook = GTK_NOTEBOOK(gtk_builder_get_object(builder, "notebook"));
	g_assert(notebook != NULL);
	
	page_num = gtk_notebook_get_current_page(notebook);
	g_assert(page_num >= 0);
	
	current_page = G_OBJECT(gtk_notebook_get_nth_page(notebook, page_num));
	g_assert(current_page != NULL);
	
	page_builder = GTK_BUILDER(g_object_get_data(current_page, "page_builder"));
	g_assert(page_builder != NULL);
	
	fill_master_notebook_page(page_builder);
	
	// Also, since the view has been refreshed, make the eventbox go bye bye.
	// It might be arguable that this should be inside fill_master_notebook_page. And yes, I agree with you.
	event_box = GTK_WIDGET(gtk_builder_get_object(builder, "editor_notebook_page_master_hint_event_box"));
	g_assert(event_box != NULL);
	
	gtk_widget_hide(event_box);
	*/
}

void
main_window_toolbar_execute_clicked_cb(GtkObject *object, gpointer user_data)
{
	/*
	GtkBuilder *builder, *page_builder;
	GtkNotebook *notebook;
	GObject *current_page;
	gint page_num;
	
	g_assert(user_data != NULL);
	
	builder = GTK_BUILDER(user_data);
	g_assert(builder != NULL);
	
	notebook = GTK_NOTEBOOK(gtk_builder_get_object(builder, "editor_notebook"));
	g_assert(notebook != NULL);
	
	page_num = gtk_notebook_get_current_page(notebook);
	g_assert(page_num >= 0);
	
	if(!main_window_notebook_is_nth_page_type(notebook, current_page, MWNPT_EDITOR))
	{
		log_error("Say whatever once more! I dare you! I double dare you!");
		return;
	}
	
	// Action not supported
	if(current_page == 0)
		return;
	
	current_page = G_OBJECT(gtk_notebook_get_nth_page(notebook));
	g_assert(current_page != NULL);
	
	page_builder = GTK_BUILDER(g_object_get_data(current_page, "page_builder"));
	g_assert(page_builder != NULL);
	
	
	// TODO: Reimplement the code below!
	
	page_builder = GTK_BUILDER(gtk_builder_get
	
	GtkPaned *paned_box = GTK_PANED(gtk_notebook_get_nth_page(notebook, current_page));
	g_assert(paned_box != NULL);
	
	GtkScrolledWindow *source_scrolled_window = GTK_SCROLLED_WINDOW(gtk_paned_get_child1(paned_box));
	g_assert(source_scrolled_window != NULL);
	
	GtkBox *bottom_box = GTK_BOX(gtk_paned_get_child2(paned_box));
	g_assert(bottom_box != NULL);
	
	GList *box_children = gtk_container_get_children(GTK_CONTAINER(bottom_box));
	g_assert(box_children != NULL);
	
	GtkScrolledWindow *tree_view_scrolled_window = GTK_SCROLLED_WINDOW(g_list_nth_data(box_children, 1));
	g_assert(tree_view_scrolled_window != NULL);
	
	GtkLabel *result_label = GTK_LABEL(g_list_nth_data(box_children, 2));
	g_assert(result_label != NULL);
	
	g_list_free(box_children);
	
	GtkTextView *text_view = GTK_TEXT_VIEW(gtk_bin_get_child(GTK_BIN(source_scrolled_window)));
	g_assert(text_view != NULL);
	
	GtkTreeView *tree_view = GTK_TREE_VIEW(gtk_bin_get_child(GTK_BIN(tree_view_scrolled_window)));
	g_assert(tree_view != NULL);
	
	GtkTextBuffer *text_buffer = gtk_text_view_get_buffer(text_view);
	g_assert(text_buffer != NULL);
	
	GtkTextIter start, end;
	
	if(gtk_text_buffer_get_has_selection(text_buffer))
	{
		gtk_text_buffer_get_selection_bounds(text_buffer, &start, &end);
	}
	else
	{
		gtk_text_buffer_get_bounds(text_buffer, &start, &end);
	}
	
	gchar *query = gtk_text_buffer_get_text(text_buffer, &start, &end, TRUE);
	g_assert(query != NULL);
	
	GtkWindow *window = GTK_WINDOW(gtk_builder_get_object(builder, "main_window"));
	g_assert(window != NULL);
	
	sqlite3 *db = sqlitegtk_get_database(window);
	g_assert(db != NULL);
	
	struct_sqlite_callback struct_data;
	struct_data.store = NULL;
	struct_data.columns_initialized = FALSE;
	struct_data.view = tree_view;
	
	gchar *error_message = NULL;
	
	GList *columns = gtk_tree_view_get_columns(tree_view);
	g_list_foreach(columns, remove_tree_view_column, tree_view);
	g_list_free(columns);
	
	GTimer *execution_timer = g_timer_new();
	g_assert(execution_timer != NULL);
	
	if(sqlite3_exec(db, query, sql_execute_query_callback, &struct_data, &error_message) == SQLITE_OK)
	{
		g_timer_stop(execution_timer);
		
		gchar *message = g_strdup_printf("Query finished successfully. Execution time: %.2f seconds", g_timer_elapsed(execution_timer, NULL));
		g_assert(message != NULL);
		
		gtk_label_set_text(result_label, message);
		
		g_free(message);
	}
	else
	{
		g_timer_stop(execution_timer);
		
		gchar *message = g_strdup_printf("An error occurred. Message: %s. Execution time: %.2f seconds", 
			(error_message == NULL ? "Unknown error" : error_message), g_timer_elapsed(execution_timer, NULL));
		g_assert(message != NULL);
		
		gtk_label_set_text(result_label, message);
		
		g_free(message);
		
		if(error_message)
			sqlite3_free(error_message);
	}
	
	if(should_trigger_hint_refresh(query))
		trigger_hint_refresh(builder);
	
	g_free(query);
	
	gtk_widget_show_all(GTK_WIDGET(bottom_box));
	
	if(struct_data.store)
	{
		gtk_tree_view_set_model (GTK_TREE_VIEW (tree_view), GTK_TREE_MODEL(struct_data.store));
		g_object_unref(struct_data.store);
	}
	*/
}

void
main_window_toolbar_new_clicked_cb(GtkObject *object, gpointer user_data)
{
	/*
	g_assert(user_data != NULL);
	
	GtkBuilder *builder = GTK_BUILDER(user_data);
	g_assert(builder != NULL);
	
	GtkNotebook *notebook = GTK_NOTEBOOK(gtk_builder_get_object(builder, "editor_notebook"));
	g_assert(notebook != NULL);
	
	main_window_create_notebook_text_page(builder, NULL, TRUE);
	*/
}

void
main_window_toolbar_open_clicked_cb(GObject *object, gpointer user_data)
{
	/*
	g_assert(user_data != NULL);
	
	struct_main_window_controls *controls_state = get_main_window_controls(user_data, EMWC_MAIN_WINDOW | EMWC_NOTEBOOK);
	g_assert(controls_state != NULL);
	g_assert(controls_state->main_window != NULL);
	
	gchar *file_path = show_open_file_dialog(controls_state->main_window, "Open SQL File", "SQL Files", text / * , NULL);
	g_assert(file_path != NULL);
	
	GtkBuilder *builder = GTK_BUILDER(user_data);
	g_assert(builder != NULL);
	
	main_window_create_notebook_text_page(builder, file_path, TRUE);
	
	g_free(file_path);
	g_free(controls_state);
	*/
}

